<?php

namespace App\Events\Chat;

use App\Events\Command;

class FriendCommand extends Command
{
    // 说话
    public function chat($client_id, $data)
    {
        // 可以对好友说话
        // 也可以对自己说话
        try{
            // 如果用户id不存在
            $this->throwIf(empty($data['uid']), '请传递你的用户id！');
            $this->throwIf(empty($data['f_uid']), '请传递你的好友用户id！');
            $this->throwIf(empty($data['say']), '请传递你要说的话！');
            
            // 保存聊天记录
            // 跨域请求
            $send   = ['uid' => $data['uid'], 'f_uid' => $data['f_uid'], 'message' => $data['say'],];
            $other  = ['model' => 'chat_friend_message'];
            $result = $this->curlChat('friend_message@add', $other, [], $send);
            $json   = json_decode($result, 1);
            $code   = isset($json['code']) ? $json['code'] : '';
            
            // 跨域返回消息
            $this->outputSend('跨域请求返回消息', $json['msg']);
            //$this->sendToCurrentClient($json);
            // 如果成功 发送消息
            if ($code == 200) {
                $say = $this->success(['uid' => $data['uid'], 'f_uid' => $data['f_uid'], 'message' => $data['say'], 'id' => $json['data'],]);
                $this->sendToUid($data['f_uid'], $say);
                $this->sendToCurrentClient($say);
            }
        } catch (Exception $e){
            $this->errorJsonToCurrentClient($e->getMessage(), $e->getCode());
        }
    }
    
    // 获取聊天记录
    
}